package yanyan.com.tochar.adpter;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


import java.util.List;
import java.util.Random;

import yanyan.com.tochar.R;
import yanyan.com.tochar.beans.HomeListBean;
import yanyan.com.tochar.gif.ImageUtil;

public class HomeListAdapter extends BaseAdapter {
    private List<HomeListBean> listBeans;
    private LayoutInflater inflater;
    private Context context;

    public HomeListAdapter(Context context, List<HomeListBean> listBeans){
        this.context=context;
        this.listBeans=listBeans;
        inflater=LayoutInflater.from(context);
    }
    @Override
    public int getCount() {
        return listBeans.size();
    }

    @Override
    public Object getItem(int position) {
        return listBeans.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if(convertView==null){
            viewHolder=new ViewHolder();
            convertView=inflater.inflate(R.layout.item_home_list,null);
            viewHolder.colorText=(TextView)convertView.findViewById(R.id.item_home_list_colorBar);
            viewHolder.title=(TextView) convertView.findViewById(R.id.item_home_list_title);
            viewHolder.text=(TextView) convertView.findViewById(R.id.item_home_list_text);
            convertView.setTag(viewHolder);
        }else {
            viewHolder= (ViewHolder) convertView.getTag();
        }

             HomeListBean homeListBean = listBeans.get(position);
            viewHolder.colorText.setBackgroundColor(this.getColor());
            viewHolder.title.setText(homeListBean.getTitle());
            viewHolder.text.setText(homeListBean.getText());
            return  convertView;

    }

    private int getColor() {
        Random random = new Random();
        int r = random.nextInt(255-90)+90;
        int g = random.nextInt(255-90)+90;
        int b = random.nextInt(255-90)+90;
//        for (int i = 0; i < 2; i++) {
//        int temp = random.nextInt(16);
//        r = r * 16 + temp;
//        temp = random.nextInt(16);
//        g = g * 16 + temp;
//        temp = random.nextInt(16);
//        b = b * 16 + temp;
//    }
    return Color.rgb(r,g,b);
    }


    class ViewHolder{
        public TextView colorText,title,text;
    }
}
